package com.csun.nusing.ui.familyactivity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.FamilyDeviceListJsonBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.activity.BedDetailActivity;
import com.csun.nusing.ui.activity.FollowOldSelectActivity;
import com.csun.nusing.ui.activity.WatchDetailActivity;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.util.SPUtils;
import com.csun.zxing.CaptureActivity;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所以设备界面 ClassName: AllDeviceActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-11-2
 */
public class FamilyAllDeviceActivity extends BaseActivity<CommonPresenter> {

	private ListView lv;
	private String type;
	private String authorization;
	protected ImageView iconiv;
	protected TextView codetv;
	protected TextView nametv;
	protected TextView timetv;
	protected Date date;
	protected FamilyDeviceListJsonBean devicejsonbean;
	private ImageView addiv;
	protected String deviceMacAddress="";
	private TextView addtv;
	private final static int SCANFLAG = 1;
	private final static int addFLAG = 2;


	@Override
	public int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_family_deviceist;
	}

	@Override
	public void initSubViews(View view) {
		// TODO Auto-generated method stub
		super.initSubViews(view);
		initView();
	}

	private void initView() {
		lv = findViewById(R.id.activity_alldevice_list);
		addiv = findViewById(R.id.family_devicelist_add_iv);
		addtv = findViewById(R.id.family_adddevice_add_tv);
		addiv.setOnClickListener(new click());
		addtv.setOnClickListener(new click());
	}

	@Override
	public void initData() {
		super.initData();
		authorization = (String) SPUtils.get(this, "authorization", "");
		requestListData();
	}

	/**
	 * 添加设备
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-17
	 */
	public void addDevice() {
		Intent intent = new Intent(this, FamilyDeviceAddActivity.class);
		startActivityForResult(intent, 0x11);
	}

	private class click implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.family_devicelist_add_iv:
				ScanDevice();
				break;
			case R.id.family_adddevice_add_tv:
				addDevice();
				break;
			}
		}
	}
	/**
	 * 扫码设备
	 */
	private void ScanDevice() {
		Intent intent = new Intent(this, CaptureActivity.class);
		startActivity(intent);
	}

	/**
	 * 加载随访仪数据
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	private void requestListData() {
		Map<String, String> param = new HashMap<String, String>();
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							devicejsonbean = gson.fromJson(result,
									FamilyDeviceListJsonBean.class);
							if (devicejsonbean != null) {
								if (devicejsonbean.getData() != null) {
									controlfollowList(devicejsonbean.getData());
								}
							}
						}
					}
				}).start(UrlBean.FAMILYDEVICESHOWURL);
	}

	/**
	 * 随访仪的数据
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-2
	 */
	protected void controlfollowList(final List<FamilyDeviceListJsonBean.AllDeviceDataBean> list) {

		MyBaseAdapter<FamilyDeviceListJsonBean.AllDeviceDataBean> myBaseAdapter = new MyBaseAdapter<FamilyDeviceListJsonBean.AllDeviceDataBean>(
				this, list, R.layout.item_device_list) {
			@Override
			public void convert(ViewHolder helper, int position,
								FamilyDeviceListJsonBean.AllDeviceDataBean item) {
				iconiv = helper
						.getView(R.id.item_device_list_device_icon);
				codetv = helper
						.getView(R.id.item_device_list_device_code_tv);
				nametv = helper
						.getView(R.id.item_device_list_device_name_tv);
				timetv = helper
						.getView(R.id.item_device_list_device_time_tv);
				if (item != null) {
					if (item.getDeviceType() == 0) {
						// 随访仪
						iconiv.setImageResource(R.drawable.shebei_icon_sfy_js);
						codetv.setText(getString(R.string.device_follow)+"    " +(item.getDeviceNo()!=null?item.getDeviceNo():"-"));
					} else if (item.getDeviceType() == 3) {
						// 报警器
						iconiv.setImageResource(R.drawable.shebei_icon_baojing_js);
						codetv.setText(getString(R.string.device_alarm)+"    " + (item.getDeviceNo()!=null?item.getDeviceNo():"-"));
					} else if (item.getDeviceType() == 1) {
						// 手表
						iconiv.setImageResource(R.drawable.shebei_icon_shoubiao_js);
						codetv.setText(getString(R.string.device_watch)+"    " + (item.getDeviceNo()!=null?item.getDeviceNo():"-"));
					} else if (item.getDeviceType() == 2) {
						// 床垫
						iconiv.setImageResource(R.drawable.shebei_icon_chuangdian_js);
						codetv.setText(getString(R.string.device_sleep)+"    " + (item.getDeviceNo()!=null?item.getDeviceNo():"-"));
					}
					nametv.setText("" + (item.getDeviceInfo()!=null&&item.getDeviceInfo().length()>0?item.getDeviceInfo():"-"));
					timetv.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getCreateTime()));
				} else {
					iconiv.setVisibility(View.GONE);
					codetv.setVisibility(View.GONE);
					nametv.setVisibility(View.GONE);
					timetv.setVisibility(View.GONE);
				}
			}
		};
		lv.setAdapter(myBaseAdapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (list.get(position) != null) {
					if (list.get(position).getDeviceType() == 0) {
						Intent intent = new Intent(
								FamilyAllDeviceActivity.this,
								FollowOldSelectActivity.class);
						intent.putExtra("deviceId", ""
								+ list.get(position).getDeviceId());
						if (list.get(position).getDeviceMac()!=null) {
							String[] listss = list.get(position).getDeviceMac().split(";");
							if (listss!=null&&listss.length>0) {
								deviceMacAddress = listss[0];
							}
						}
						intent.putExtra("deviceMacAddress",deviceMacAddress);
						intent.putExtra("isFamily", true);
						startActivity(intent);
					} else if (list.get(position).getDeviceType() == 3) {
						Intent intent = new Intent(
								FamilyAllDeviceActivity.this,
								FamilyAlarmDetailActivity.class);
						intent.putExtra("deviceId", ""
								+ list.get(position).getDeviceId());
						startActivity(intent);
					} else if (list.get(position).getDeviceType() == 1) {
						Intent intent = new Intent(
								FamilyAllDeviceActivity.this,
								WatchDetailActivity.class);
						intent.putExtra("isFamily", true);
						intent.putExtra("deviceId", ""
								+ list.get(position).getDeviceId());
						startActivity(intent);
					} else if (list.get(position).getDeviceType() == 2) {
						Intent intent = new Intent(
								FamilyAllDeviceActivity.this,
								BedDetailActivity.class);
						intent.putExtra("isFamily", true);
						intent.putExtra("deviceId", ""
								+ list.get(position).getDeviceId());
						startActivity(intent);
					}
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		requestListData();
	}

}
